package com.ruoyi.web.util;

import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.net.URLEncoder;
import java.util.Iterator;
import java.util.Map;

public class WebUtil {

    /**
     * 发送POST请求
     *
     * @param url    请求的接口路径
     * @param params 参数
     * @return
     * @throws
     */
    public static String post(String url, Map<String, String> params, String charset) throws IOException {
        CloseableHttpClient client = HttpClients.createDefault();
        StringBuilder stringBuilder = new StringBuilder(url);
        if (params!=null&&!params.isEmpty()) {
            boolean hasParam = false;
            Iterator<String> it = params.keySet().iterator();
            while (it.hasNext()) {
                String name = it.next();
                String value = params.get(name);
                if (areNotEmpty(name, value)) {
                    if (hasParam) {
                        stringBuilder.append("&");
                    } else {
                        hasParam = true;
                    }
                    stringBuilder.append(name).append("=").append(URLEncoder.encode(value, charset));
                }
            }
        }

        HttpPost httpPost = new HttpPost(stringBuilder.toString());
        httpPost.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36)");
        //发送请求返回响应的信息
        CloseableHttpResponse response = client.execute(httpPost);
        HttpEntity entity = response.getEntity();
        if (entity != null) {
            String result = EntityUtils.toString(entity, "UTF-8");
            return result;
        }
        return null;
    }

    public static String get(String url, Map<String, String> params, String charset) throws IOException {
        CloseableHttpClient client = HttpClients.createDefault();
        StringBuilder stringBuilder = new StringBuilder(url);
        if (!params.isEmpty()) {
            boolean hasParam = false;
            Iterator<String> it = params.keySet().iterator();
            while (it.hasNext()) {
                String name = it.next();
                String value = params.get(name);
                if (areNotEmpty(name, value)) {
                    if (hasParam) {
                        stringBuilder.append("&");
                    } else {
                        hasParam = true;
                    }
                    stringBuilder.append(name).append("=").append(URLEncoder.encode(value, charset));
                }
            }
        }

        HttpGet httpGet = new HttpGet(stringBuilder.toString());
        httpGet.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36)");
        //发送请求返回响应的信息
        CloseableHttpResponse response = client.execute(httpGet);
        HttpEntity entity = response.getEntity();
        if (entity != null) {
            String result = EntityUtils.toString(entity, "UTF-8");
            return result;
        }
        return null;
    }




    public static boolean isEmpty(String value) {
        int strLen;
        if (value != null && (strLen = value.length()) != 0) {
            for (int i = 0; i < strLen; ++i) {
                if (!Character.isWhitespace(value.charAt(i))) {
                    return false;
                }
            }

            return true;
        } else {
            return true;
        }
    }

    public static boolean areNotEmpty(String... values) {
        boolean result = true;
        if (values != null && values.length != 0) {
            String[] var2 = values;
            int var3 = values.length;

            for (int var4 = 0; var4 < var3; ++var4) {
                String value = var2[var4];
                result &= !isEmpty(value);
            }
        } else {
            result = false;
        }

        return result;
    }
}
